package launch;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;

import pgui.PVisual;
import rpg.Rpg;
import rpg.data.CreatureXML;
import rpg.data.EffectXML;
import rpg.data.ItemXML;
import rpg.data.MapXML;
import rpg.data.SkillXML;
import rpg.data.SpriteXML;
import rpg.data.XML;
import rpg.visual.VisualImage;
import rpg.visual.mapEditor.VisualList;
import rpg.visual.mapEditor.VisualMapEditor;
import rpg.visual.mapEditor.VisualOptions;
import square.data.DataGroup;
import square.data.MapData;

/**
 * 
 * @author Pedro Silva Moreira
 */
public class LaunchMapEditor extends JFrame {
	private static final long serialVersionUID = 1L;

	// Local Variables
	private PVisual visual;
	private VisualOptions visualOptions;

	private VisualList visualMapObjectList;

	private JScrollPane visualMapScrollPane;
	private VisualMapEditor visualMap;

	// Creation
	public LaunchMapEditor() {
		super();
		Rpg.disableEvents = true;

		this.setTitle(LaunchConfig.getName() + " - "
				+ LaunchConfig.getVersion() + " | Created By "
				+ LaunchConfig.getCreatorsCompact());
		this.setSize(960, 620);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);

		// Visual Map
		visual = new PVisual(60);
		visualMapScrollPane = new JScrollPane();
		visualMapScrollPane.getViewport().add(visual);
		visual.setBackground(Color.black);
		
		visualMapScrollPane.setPreferredSize(new Dimension(Short.MAX_VALUE,
				Short.MAX_VALUE));
		
		visualMap = new VisualMapEditor(visual);
		visualMap.setScroll(true);
		visual.getMainComponent().addChild(visualMap);
		
		// Toolbar
		visualOptions = new VisualOptions(visualMap);

		// Map Object List
		visualMapObjectList = new VisualList(visualMap,
				visualOptions);

		// Vertical Panel
		JPanel verticalPanel = new JPanel();
		this.add(verticalPanel);

		verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));

		// Toolbar
		verticalPanel.add(visualOptions);
		verticalPanel.add(new JSeparator());

		verticalPanel.add(Box.createVerticalStrut(4));

		// Horizontal Panel
		JPanel horizontalPanel = new JPanel();
		verticalPanel.add(horizontalPanel);

		horizontalPanel.setLayout(new BoxLayout(horizontalPanel,
				BoxLayout.X_AXIS));

		// Map Object List
		horizontalPanel.add(visualMapObjectList);

		// Visual Map
		horizontalPanel.add(visualMapScrollPane);
	}

	public final void loadData() {		
		XML.load(SpriteXML.instance(), "sprites", "sprite");
		XML.load(EffectXML.instance(), "effects", "effect");
		XML.load(ItemXML.instance(), "items", "item");
		XML.load(SkillXML.instance(), "skills", "skill");
		XML.load(CreatureXML.instance(), "creatures", "creature");
		XML.load(MapXML.instance(), "maps", "map");

		loadMap(DataGroup.getMaps().get(0));
	}

	public final void loadMap(MapData map) {
		visual.setPreferredSize(new Dimension(map.getWidth()
				* VisualImage.SQUARE_SIZE, map.getHeight()
				* VisualImage.SQUARE_SIZE));
		visual.setMinimumSize(new Dimension(map.getWidth()
				* VisualImage.SQUARE_SIZE, map.getHeight()
				* VisualImage.SQUARE_SIZE));
		visual.setMaximumSize(new Dimension(map.getWidth()
				* VisualImage.SQUARE_SIZE, map.getHeight()
				* VisualImage.SQUARE_SIZE));
		visual.setSize(new Dimension(map.getWidth() * VisualImage.SQUARE_SIZE,
				map.getHeight() * VisualImage.SQUARE_SIZE));

		visualMap.start();
		visualMapObjectList.loadSwingModels();

		visualMapScrollPane.getVerticalScrollBar().setUnitIncrement(VisualImage.SQUARE_SIZE);
	}

	// Main
	public static void main(String[] args) {
		LaunchMapEditor launchMapEditor = new LaunchMapEditor();
		launchMapEditor.setVisible(true);

		launchMapEditor.loadData();
	}

	// Get & Set
	public PVisual getVisual() {
		return visual;
	}

	public void setVisual(PVisual visual) {
		this.visual = visual;
	}

	public VisualOptions getVisualOptions() {
		return visualOptions;
	}

	public void setVisualOptions(VisualOptions visualOptions) {
		this.visualOptions = visualOptions;
	}

	public VisualList getVisualMapObjectList() {
		return visualMapObjectList;
	}

	public void setVisualMapObjectList(
			VisualList visualMapObjectList) {
		this.visualMapObjectList = visualMapObjectList;
	}

	public VisualMapEditor getVisualMap() {
		return visualMap;
	}

	public void setVisualMap(VisualMapEditor visualMap) {
		this.visualMap = visualMap;
	}

	public JScrollPane getVisualMapScrollPane() {
		return visualMapScrollPane;
	}

	public void setVisualMapScrollPane(JScrollPane visualMapScrollPane) {
		this.visualMapScrollPane = visualMapScrollPane;
	}
}
